<template>
  <div class="table-page">
    <GiTable
      row-key="id"
      title="比赛项目管理"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #custom-left>
	        <a-input v-model="queryForm.name" placeholder="请输入项目名称" allow-clear @change="search">
	          <template #prefix><icon-search /></template>
	        </a-input>
	        <a-input v-model="queryForm.type" placeholder="请输入比赛类型" allow-clear @change="search">
	          <template #prefix><icon-search /></template>
	        </a-input>
	        <a-input v-model="queryForm.gtype" placeholder="请输入男子/女子赛" allow-clear @change="search">
	          <template #prefix><icon-search /></template>
	        </a-input>
	        <a-input v-model="queryForm.place" placeholder="请输入比赛地点" allow-clear @change="search">
	          <template #prefix><icon-search /></template>
	        </a-input>
        <a-button @click="reset">重置</a-button>
      </template>
      <template #custom-right>
        <a-button v-permission="['sport:project:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <span>新增</span>
        </a-button>
      </template>
      <template #type="{ record }">
        <a-tag v-if="record.type === '0'" color="green">个人</a-tag>
        <a-tag v-else color="red">团队</a-tag>
      </template>
      <template #gtype="{ record }">
        <a-tag v-if="record.gtype === '1'" color="green">男子</a-tag>
        <a-tag v-else color="red">女子</a-tag>
      </template>
      <template #isCancel="{ record }">
        <a-tag v-if="record.isCancel === 0" color="green">未取消</a-tag>
        <a-tag v-else color="red">已取消</a-tag>
      </template>
      <template #name="{ record }">
        <a-link @click="onDetail(record)">{{ record.name }}</a-link>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link @click="searchList(record.id)">查看参赛名单</a-link>
          <a-link @click="searchReferee(record.id)">查看裁判名单</a-link>
          <a-link @click="searchResult(record.id)">录入成绩</a-link>
          <a-link v-permission="['sport:project:update']" @click="onUpdate(record)">修改</a-link>
          <a-link
            v-permission="['sport:project:delete']"
            status="danger"
            :disabled="record.disabled"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <ProjectAddModal ref="ProjectAddModalRef" @save-success="search" />
    <ProjectDetailDrawer ref="ProjectDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import ProjectAddModal from './ProjectAddModal.vue'
import ProjectDetailDrawer from './ProjectDetailDrawer.vue'
import { type ProjectResp, type ProjectQuery, deleteProject, exportProject, listProject } from '@/apis'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useDownload, useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import { useDict } from '@/hooks/app'
import router from "@/router";

defineOptions({ name: 'Project' })

const queryForm = reactive<ProjectQuery>({
  name: "",
  rule: "",
  type: "",
  gtype: "",
  refereeNumber: "",
  place: "",
  time: "",
  isCancel: undefined,
  sort: ['createTime,desc']
})


const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listProject({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  { title: '项目名称', dataIndex: 'name', slotName: 'name' },
  { title: '比赛类型 ', dataIndex: 'type', slotName: 'type' },
  { title: '男子/女子赛 ', dataIndex: 'gtype', slotName: 'gtype' ,},
  { title: '参赛人数/队数', dataIndex: 'entriesNumber', slotName: 'entriesNumber',width: 150 },
  { title: '裁判员数量', dataIndex: 'refereeNumber', slotName: 'refereeNumber' },
  { title: '比赛地点', dataIndex: 'place', slotName: 'place' },
  { title: '比赛时间', dataIndex: 'time', slotName: 'time' ,width: 190 },
  { title: '状态', dataIndex: 'isCancel', slotName: 'isCancel' },
  {
    title: '操作',
    slotName: 'action',
    width: 450,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['sport:project:update', 'sport:project:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.name = ""
  queryForm.rule = ""
  queryForm.type = ""
  queryForm.gtype = ""
  queryForm.place = ""
  search()
}

// 查看参赛名单
const searchList = (id: string) => {
  console.log("id: " + id)
  router.push({
    path: '/projectentry',
    query: {id}
  })
}

// 查看裁判名单
const searchReferee = (id: string) => {
  router.push({
    path: '/sport/projectReferee',
    query: {id}
  })
}

const searchResult = (id: string) => {
  router.push({
    path: "/projectEntryGrade",
    query: {id}
  })
}

// 删除
const onDelete = (item: ProjectResp) => {
  return handleDelete(() => deleteProject(item.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

const ProjectAddModalRef = ref<InstanceType<typeof ProjectAddModal>>()
// 新增
const onAdd = () => {
  ProjectAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (item: ProjectResp) => {
  ProjectAddModalRef.value?.onUpdate(item.id)
}

const ProjectDetailDrawerRef = ref<InstanceType<typeof ProjectDetailDrawer>>()
// 详情
const onDetail = (item: ProjectResp) => {
  ProjectDetailDrawerRef.value?.onDetail(item.id)
}
</script>

<style lang="scss" scoped></style>
